package com.gopay.common.util;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import java.security.SecureRandom;

/**
 * AES加解密 使用PKCS5Padding填充模式
 * @author <a href="mailto:yhwpeng@126.com">wangdong</a>
 * @date 2017-04-26 16:44:10
 */
public class AESUtils {

	protected final static Log logger = LogFactory.getLog(AESUtils.class);

	private AESUtils() {
	}

	/**
	 * 加密
	 * @author <a href="mailto:yhwpeng@126.com">wangdong</a>
	 * @date 2017-04-27 10:57:51
	 */
	public static byte[] AES_CBC_Encrypt(byte[] content, byte[] keyBytes, byte[] iv) {
		try{
			KeyGenerator keyGenerator=KeyGenerator.getInstance("AES");
			keyGenerator.init(128, new SecureRandom(keyBytes));
//			SecretKey keyspec=keyGenerator.generateKey();
			SecretKeySpec keyspec = new SecretKeySpec(keyBytes, "AES");
			Cipher cipher=Cipher.getInstance("AES/CBC/PKCS5Padding");
			cipher.init(Cipher.ENCRYPT_MODE, keyspec, new IvParameterSpec(iv));
			byte[] result=cipher.doFinal(content);
			return result;
		}catch (Exception e) {
			System.out.println("exception:"+e.toString());
		}
		return null;
	}

	/**
	 * 解密
	 * @author <a href="mailto:yhwpeng@126.com">wangdong</a>
	 * @date 2017-04-27 10:57:43
	 */
	public static byte[] AES_CBC_Decrypt(byte[] content, byte[] keyBytes, byte[] iv){
		try{
			KeyGenerator keyGenerator=KeyGenerator.getInstance("AES");
			keyGenerator.init(128, new SecureRandom(keyBytes));//key长可设为128，192，256位，这里只能设为128
//			SecretKey keyspec=keyGenerator.generateKey();
			SecretKeySpec keyspec = new SecretKeySpec(keyBytes, "AES");
			Cipher cipher=Cipher.getInstance("AES/CBC/PKCS5Padding");
			cipher.init(Cipher.DECRYPT_MODE, keyspec, new IvParameterSpec(iv));
			byte[] result=cipher.doFinal(content);
			return result;
		}catch (Exception e) {
			System.out.println("exception:"+e);
		}
		return null;
	}

	public static String byteToHexString(byte[] bytes) {
		StringBuffer sb = new StringBuffer(bytes.length);
		String sTemp;
		for (int i = 0; i < bytes.length; i++) {
			sTemp = Integer.toHexString(0xFF & bytes[i]);
			if (sTemp.length() < 2) {
                sb.append(0);
            }
			sb.append(sTemp.toUpperCase());
		}
		return sb.toString();
	}

	public static void main(String[] args) {
		String content="a";
		String key="0123456789012345";
		String iv="0123456789012345";
		try {
			System.out.println("加密前："+byteToHexString(content.getBytes()));
			byte[ ] encrypted=AES_CBC_Encrypt(content.getBytes(), key.getBytes(), iv.getBytes());
			System.out.println("加密后："+byteToHexString(encrypted));
			byte[] encode = Base64.encode(encrypted);
			System.out.println("加密后Base64："+new String(encode));
			byte[] decrypted=AES_CBC_Decrypt(encrypted, key.getBytes(), iv.getBytes());
			System.out.println("解密后："+byteToHexString(decrypted));
			System.out.println("解密后："+new String(decrypted));
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
